package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.cjst.dao.mapper.sys.SysMenuMapper;
import com.cjst.pojo.sys.SysMenu;
import com.cjst.pojo.sys.SysRelation;
import com.cjst.pojo.sys.SysRole;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.IsMenu;
import com.cjst.sdp.util.MenuNode;
import com.cjst.sdp.util.MenuStatus;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysMenuService;
import com.cjst.service.sys.SysRelationService;
import com.cjst.service.sys.SysRoleService;
import com.cjst.service.sys.SysUserService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.CloneClass;


/**
 * 菜单
 * @author yj
 *
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu, String> implements SysMenuService{
	
	@Autowired
	private SysMenuMapper menuDao;
	
	@Autowired
	private SysRelationService relationService;
	
	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SysRoleService roleService;
	@Value("${file.path}")
	private String filePath;
	
	@Override
	public List<MenuNode> menuRight(SysUser sysUser,Integer isNose,Integer isTop){
		if(isTop==null){
			isTop=0;
		}
		List<MenuNode> list=new ArrayList<MenuNode>();
		String roleId = sysUser.getRoleId();
		if(StringUtils.isNotEmpty(roleId)) {
			SysRole role = roleService.get(roleId);
			if(role!=null) {
				if("admin".equals(role.getName())) { //如果角色是超级管理员，就直接查询所有菜单权限
					WherePrams wherePrams=Method.where("status", C.EQ, "1");
					wherePrams.and("isNose", C.EQ, isNose);
					if(isNose==1){
						wherePrams.orderBy(" num asc ");
					}
					List<SysMenu> menuList=this.list(wherePrams);
					if(menuList!=null && menuList.size()>0){
						for (SysMenu sysMenu : menuList) {
							
							
							/*
							 * 临时代码
							 */
							
							/*
							if(sysMenu.getMenuType()==0) {
								MenuNode_tmp menuNode=new MenuNode_tmp();
								SysMenu_tmp sysMenu_tmp=new SysMenu_tmp();
								CloneClass.getClone(sysMenu, sysMenu_tmp);
								sysMenu_tmp.setSys_value(12.3456);
								CloneClass.getClone(sysMenu_tmp, menuNode);
								
								menuNode.setId(sysMenu.getId());
								list.add(menuNode);
								
							}
							else if(sysMenu.getMenuType()==1) {
								MenuNode menuNode=new MenuNode();
								CloneClass.getClone(sysMenu, menuNode);
								menuNode.setId(sysMenu.getId());
								list.add(menuNode);
							}
							else {
								MenuNode menuNode=new MenuNode();
								CloneClass.getClone(sysMenu, menuNode);
								menuNode.setId(sysMenu.getId());
								list.add(menuNode);
								
							}
							*/
							/*
							 * 
							 */
							
							
							 //原始代码
							
							MenuNode menuNode=new MenuNode();
							CloneClass.getClone(sysMenu, menuNode);
							menuNode.setId(sysMenu.getId());
							list.add(menuNode);
							
						}
					}
				}else { 
					//1.非超级管理员，就根据角色查询对应的权限
					WherePrams where =Method.where("roleid", C.EQ, roleId);
					if(isTop.equals(1)){
						where.and("isTopOpen", C.EQ, 1);
					}
					List<SysRelation> relations = relationService.list(where);
					
					//2.根据权限，查询用户菜单
					List<String> menuIds = new ArrayList<>();
					//menuIds.add("");
					for (SysRelation sysRelation : relations) {
						menuIds.add(sysRelation.getMenuid());
					}
					WherePrams menuPrams = Method.where("status", C.EQ, "1");
					menuPrams.and("isNose", C.EQ, isNose);
					menuPrams.andIn("id", C.IN, menuIds);
					if(isNose==1){
						menuPrams.orderBy(" num asc ");
					}
					List<SysMenu> menus = this.list(menuPrams);
					if(menus!=null&&menus.size()>0) {
						for (SysMenu sysMenu : menus) {
							MenuNode menuNode=new MenuNode();
							CloneClass.getClone(sysMenu, menuNode);
							menuNode.setId(sysMenu.getId());
							list.add(menuNode);
						}
					}
						
				}
					
				
			}
		}
		
		
		/*List<SysMenu> menuList=this.list(WherePrams);
		if(menuList!=null && menuList.size()>0){
			for (SysMenu sysMenu : menuList) {
				MenuNode menuNode=new MenuNode();
				CloneClass.getClone(sysMenu, menuNode);
				menuNode.setId(sysMenu.getId());
				list.add(menuNode);
			}
		}*/
		return list;
	}
	
	
	@Override
	public List<ZTreeNode> tree(){
		WherePrams wherePrams=Method.where("status", C.EQ, "1");
		List<SysMenu> sysMenuList=this.list(wherePrams);
		List<ZTreeNode> nodeList=new ArrayList<ZTreeNode>();
		if(sysMenuList!=null && sysMenuList.size()>0){
			for (SysMenu sysMenu : sysMenuList) {
				ZTreeNode node=new ZTreeNode();
				CloneClass.getClone(sysMenu, node);
				node.setId(sysMenu.getId());
				node.setpId(sysMenu.getParentId());
				node.setName(sysMenu.getName());
				nodeList.add(node);
			}
		}
		return nodeList;
	}

	@Override
	public List<SysMenu> selectMenus(String condition, String level) {
		SysMenu menu = new SysMenu();
		menu.setCode(condition);
		menu.setName(condition);
		if(StringUtils.isNotEmpty(level)){
			menu.setLevels(Integer.parseInt(level));
		}		
		return menuDao.selectMenus(menu);
	}

	/**
	 * 由于bootstraptreetable不支持formatter函数，bean又不能随便添加属性，所以用map接收返回值
	 * xfc
	 */
	@Override
	public List<Map<String, Object>> getMenuMap(String condition, String level) {
		SysMenu menu = new SysMenu();
		menu.setCode(condition);
		menu.setName(condition);
		if(StringUtils.isNotEmpty(level)){
			menu.setLevels(Integer.parseInt(level));
		}
		List<Map<String, Object>> menus = menuDao.getMenuMap(menu);
		for (Map<String, Object> map : menus) {
			Integer ismenu = (Integer)map.get("ismenu");			
			map.put("isMenuName", IsMenu.valueOf(ismenu));			
			String status = (String)map.get("status");
			if(StringUtils.isNotEmpty(status)){
				map.put("statusName", MenuStatus.valueOf(Integer.parseInt(status)));
			}
		}
		return menus;
	}
	
	/*
	@Override
	public List<MenuNode> getAllMenuByNode(String userId){
		
		
		SysUser user=sysUserService.get(userId);
		String roleId=user.getRoleId();
		WherePrams rlWhere=Method.where("isTopOpen", C.EQ, 1);
		rlWhere.and("roleid", C.EQ, roleId);
		WherePrams parms=Method.where("status", C.EQ, "1");
		List<SysRelation> relationList=relationService.list(rlWhere);
		if(relationList!=null && relationList.size()>0 && !user.getUserName().equals("admin")){
			List<String> strList=new ArrayList<String>();
			for (SysRelation sysRelation : relationList) {
				strList.add(sysRelation.getMenuid());
			}
			parms.andIn("id", C.IN, strList);
		}
		parms.and("isNose", C.EQ, 1);
		parms.and("parentId", C.EQ, "MMW9H7EYIEYP55BE5F021B84E59C4E1FA2068C975ABD157B");
		List<SysMenu> menuList=this.list(parms);
		List<MenuNode> list=new ArrayList<MenuNode>();
		if(menuList!=null && menuList.size()>0){
			for (SysMenu sysMenu : menuList) {
				MenuNode menuNode=new MenuNode();
				CloneClass.getClone(sysMenu, menuNode);
				menuNode.setId(sysMenu.getId());
				if(menuNode.getIcon()!=null && menuNode.getIcon().length()>0){
					menuNode.setIcon(filePath+menuNode.getIcon());
				}
				WherePrams parms2=Method.where("status", C.EQ, "1");
				parms2.and("isNose", C.EQ, 1);
				parms2.and("parentId", C.EQ, sysMenu.getId());
				List<SysMenu> menuList2=this.list(parms2);
				if(menuList2!=null && menuList2.size()>0){
					List<MenuNode> list2=new ArrayList<MenuNode>();
					for (SysMenu sysMenu2 : menuList2) {
						MenuNode menuNode2=new MenuNode();
						CloneClass.getClone(sysMenu2, menuNode2);
						menuNode2.setId(sysMenu2.getId());
						if(menuNode2.getIcon()!=null && menuNode2.getIcon().length()>0){
							menuNode2.setIcon(filePath+menuNode2.getIcon());
						}
						list2.add(menuNode2);
					}
					menuNode.setChildren(list2);
				}
				list.add(menuNode);
			}
		}
		return list;
	}
	*/
	
	
	@Override
	public List<MenuNode> getAllMenuByNode(String userId,Integer menuType){
		if(menuType==2){
			WherePrams tmpPrams=Method.where("status", C.EQ, 1);
			tmpPrams.and("menuType", C.EQ, menuType);
			tmpPrams.orderBy("num asc");
			List<SysMenu> menuList=this.list(tmpPrams);
			List<MenuNode> list=new ArrayList<MenuNode>();
			if(menuList!=null && menuList.size()>0){
				for (SysMenu sysMenu : menuList) {
					MenuNode menuNode=new MenuNode();
					CloneClass.getClone(sysMenu, menuNode);
					if(menuNode.getIcon()!=null && menuNode.getIcon().length()>0){
						menuNode.setIcon(filePath+menuNode.getIcon());
					}
					list.add(menuNode);
				}
			}
			return list;
		}else{
			//get roleId
			SysUser user=sysUserService.get(userId);
			String roleId=user.getRoleId();
			
			//获取所有 有权限的APP应用---SysRelation表---MenuId
			WherePrams rlWhere=Method.where("roleid", C.EQ, roleId);
			WherePrams parms=Method.where("status", C.EQ, "1");
			List<SysRelation> relationList=relationService.list(rlWhere);
			
			
			
			//根据MenuId获取所有 有权限的APP应用---Menu表
			WherePrams tmpPrams=Method.where("status", C.EQ, 1);
			if(relationList!=null && relationList.size()>0 && !user.getUserName().equals("admin")){
				List<String> strList=new ArrayList<String>();
				for (SysRelation sysRelation : relationList) {
					strList.add(sysRelation.getMenuid());
				}
				tmpPrams.andIn("id", C.IN, strList);
			}
			List<SysMenu> Allmenu=this.list(tmpPrams);
			
			
			//获取所有父菜单
			parms.and("isNose", C.EQ, 1);
			parms.and("parentId", C.EQ, "MMW9H7EYIEYP55BE5F021B84E59C4E1FA2068C975ABD157B");
			List<SysMenu> menuList=this.list(parms);
			
			//删除某条父菜单
			Iterator<SysMenu> tmp=menuList.iterator();
			while(tmp.hasNext()) {
				SysMenu a=tmp.next();
				
				if("3LE3HND3TNSHNXIWF3D5C30B0FE0472AA91E30CE4C836195".equals(a.getId())) {
					tmp.remove();
				}
			}
			
			List<MenuNode> list=new ArrayList<MenuNode>();
			if(menuList!=null && menuList.size()>0){
				for (SysMenu sysMenu : menuList) {
					//父菜单加入MenuNode
					MenuNode menuNode=new MenuNode();
					CloneClass.getClone(sysMenu, menuNode);
					menuNode.setId(sysMenu.getId());
					if(menuNode.getIcon()!=null && menuNode.getIcon().length()>0){
						menuNode.setIcon(filePath+menuNode.getIcon());
					}
					
					
					String father=sysMenu.getId();
					if(Allmenu!=null && Allmenu.size()>0){
						List<MenuNode> list2=new ArrayList<MenuNode>();
						for (SysMenu sysMenu2 : Allmenu) {
							String child=sysMenu2.getParentId();
							
							if(child.equals(father)) {
							
							MenuNode menuNode2=new MenuNode();
							CloneClass.getClone(sysMenu2, menuNode2);
							menuNode2.setId(sysMenu2.getId());
							if(menuNode2.getIcon()!=null && menuNode2.getIcon().length()>0){
								menuNode2.setIcon(filePath+menuNode2.getIcon());
							}
							list2.add(menuNode2);
							}
						}
						menuNode.setChildren(list2);
					}
					list.add(menuNode);
				}
			}
			return list;
		}
		
	}



	
	
}




